#include <cstddef>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

class Item{
public:
    string key;
    string value;

    Item(string k,string v)
        :key(k)
         ,value(v){
             
         }
};


class Config{
public:
    static Config* getInstance(){
        if(_pInstance==nullptr){
            _pInstance=new Config();
        }
        return _pInstance;
    }

    static void destory(){
        if(_pInstance){
            delete _pInstance;
            _pInstance=nullptr;
        }
    }

    void readConfig(string &filename){
        string key;
        string value;
        ifstream ifs(filename);
        string line;
        while(getline(ifs,line)){
            istringstream iss(line);
            int flag=1;
            iss>>key>>value;
            for(size_t i=0;i<vec.size();i++){
                if(vec[i].key==key){
                    vec[i].value=value;
                    flag=0;
                    break;
                }
            }
            
            if(flag){
            Item it(key,value);
            vec.emplace_back(it);
            }
        }
    }

    string getValue(string & key){
        for(size_t i=0;i<vec.size();i++){
            if(vec[i].key==key){
                return vec[i].value;
            }
            cout<<"i"<<i<<endl;
            cout<<vec[i].key<<endl;
        }
        cout<<vec.size()<<endl;
        cout<<"--- No this Key ---"<<endl;
        return " ";
    }

    void showVec(){
        for(size_t i=0;i<vec.size();i++){
            cout<<"key:"<<vec[i].key<<" value: "<<vec[i].value<<endl;
        }
    }

private:
    Config(){ vec.reserve(10);  };
    ~Config(){};
    Config(const Config &rhs)=delete;
    static Config* _pInstance;
    vector<Item> vec;
    
};
Config* Config::_pInstance = nullptr;
void test(){
    string configFile="./Config.ini";
    Config *con=Config::getInstance();
    con->readConfig(configFile);
    string ip="ip";
    ip=con->getValue(ip);
    cout<<ip<<endl;
    con->showVec();
    con->destory();
}

int main()
{
 test();
 return 0;
}

